Partial Class pages_Buttons_Button : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property ButtonId() As Integer
        Get
            Return CWeb.RequestInt("buttonId")
        End Get
    End Property
    Public ReadOnly Property IsEdit() As Boolean
        Get
            Return ButtonId <> Integer.MinValue
        End Get
    End Property
#End Region

#Region "Members"
    Private m_button As CButton
#End Region

#Region "Data"
    Public ReadOnly Property [Button]() As CButton
        Get
            If IsNothing(m_button) Then
                If IsEdit Then
                    m_button = CButton.Cache.GetById(ButtonId)
                    If IsNothing(m_button) Then CSitemap.RecordNotFound("Button", ButtonId)
                Else
                    m_button = New CButton
                    'Inserts: set parentId here (if applicable)
                End If
            End If
            Return m_button
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.ButtonEdit(Me.Button.ButtonId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Buttons)
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()

        'Page Title
        If IsEdit Then            
            Me.Title = "Button Details"
        Else
            Me.Title = "Create New Button"
        End If
                
        'Textbox logic
        'Me.Form.DefaultFocus = txtButtonName.Textbox.ClientID;
        Me.Form.DefaultButton = btnSave.UniqueID   'txtButtonName.OnReturnPress(btnSave)

        'Button Text
        btnDelete.Visible = IsEdit
        If IsEdit Then btnCancel.Text = "Back" Else btnSave.Text = "Create Button"
        If IsEdit Then AddButton(CSitemap.ButtonAdd(), "Create a new Button")
    End Sub
    Protected Overrides Sub PageLoad()
        If Page.IsPostback Then Exit Sub

        LoadButton()
    End Sub
#End Region

#Region "Event Handlers - Form"
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Not Me.IsValid() Then Exit Sub
        SaveButton()
        Refresh()
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ReturnToList()
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Me.Button.Delete()
            'CCache.ClearCache()
        ReturnToList()        
    End Sub
#End Region

#Region "Private - Load/Save"
    Protected Sub LoadButton()
        With Me.Button
            txtButtonName.Text = .ButtonName
            txtButtonPaypalCode.Text = .ButtonPaypalCode
            txtButtonPrice.ValueMoney = .ButtonPrice
        End With
    End Sub
    Protected Sub SaveButton()
        With Me.Button
            .ButtonName = txtButtonName.Text
            .ButtonPaypalCode = txtButtonPaypalCode.Text.Trim
            .ButtonPrice = txtButtonPrice.ValueMoney

            If .ButtonPaypalCode.StartsWith("http") Then
                .ButtonPaypalCode = .ButtonPaypalCode.Substring(CSitemap.External_Paypal(String.Empty).Length)
            End If
            If .ButtonPaypalCode.Length > 50 Then
                .ButtonPaypalCode = CUtilities.Truncate(.ButtonPaypalCode)
            End If

            .Save()
        End With
            'CCache.ClearCache()
    End Sub
#End Region

End Class
